package com.hdcx.Minfresh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.bean.AdressWrapper;
import com.hdcx.Minfresh.bean.UserInfoBean;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.ShpfUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/18.
 */

public class AddressActivity extends BaseActivity {
    TextView tv_phone,tv_address,tv_name,tv_title,tv_nothing,tv_notes;
    LinearLayout layout_main,layout_add;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_);
        initView();
        initData();
    }

    private void initView() {
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_nothing = (TextView) findViewById(R.id.tv_nothing);
        tv_notes = (TextView) findViewById(R.id.tv_notes);
        layout_main = (LinearLayout) findViewById(R.id.layout_main);
        layout_add = (LinearLayout) findViewById(R.id.layout_add);
        layout_add.setOnClickListener(this);
        findViewById(R.id.tv_left).setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        post_main();
    }

    private void initData() {
        tv_title.setText("收货地址");
    }
   /* uid   //用户id  20*/
    private void post_main() {
        UserInfoBean user =  ShpfUtil.getObject(ShpfUtil.LOGIN);
        JSONObject json = new JSONObject();
        try {
            json.put("uid",user.getUserinfo().getId());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.ADDRESS)
                .content(json.toString())
                .build()
                .execute(new Callback<AdressWrapper>() {
                    @Override
                    public AdressWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, AdressWrapper.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(AdressWrapper response) {
                        if (response.getStatus() == 1) {
                          setValue(response);
                        }
                    }
                });
    }

    private void setValue(AdressWrapper response) {
        layout_main.setVisibility(View.VISIBLE);
        layout_add.setVisibility(View.GONE);
        tv_address.setText(response.getUser().getAddress());
        tv_phone.setText(response.getUser().getMobile());
        tv_name.setText("收货人: "+response.getUser().getName());
        tv_notes.setText(response.getNotes());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_left:
                finish();
                break;
            case R.id.layout_add:
                startActivity(new Intent(this,AddAddressActivity.class));
                break;
        }
    }
}
